package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 添加套餐
     * @param checkgroupIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestParam ArrayList<Integer> checkgroupIds, @RequestBody Setmeal setmeal){
        setmealService.add(setmeal,checkgroupIds);

        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 修改套餐
     * @param checkGroupIds
     * @param setmeal
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestParam ArrayList<Integer> checkGroupIds, @RequestBody Setmeal setmeal){
        setmealService.update(setmeal,checkGroupIds);

        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    /**
     * 根据套餐id查询套餐信息
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Setmeal setmeal = setmealService.findById(id);

        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    /**
     * 分页查询套餐信息
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.findPage(queryPageBean);

        return pageResult;
    }

    /**
     * 删除套餐
     * @param id
     * @return
     */
    public Result delete(Integer id){

        setmealService.delete(id);

        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 上传图片
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        try {
            // 获取文件原始名称
            String originalFilename = imgFile.getOriginalFilename();
            // 获取文件后缀名
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            //生成uuid
            String uuid = UUID.randomUUID().toString();
            // 拼接文件名
            String objectName = uuid + suffix;
            // 上传文件
            String url = aliOssUtil.upload(imgFile.getBytes(), objectName);
            // 将上传的图片名称保存到redis中
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,objectName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,url);
        }catch (Exception e){
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

}
